2 * wiggle - apply rejected patches
4 * Copyright (C) 2003-2013 Neil Brown <neilb@suse.de>
5 * Copyright (C) 2014-2020 Neil Brown <neil@brown.name>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program.
22 * Email: <neil@brown.name>
26 * Split a stream into words or lines
29 * string of [A-Za-z0-9_]
31 * or single char (i.e. punctuation and newlines).
33 * A line is any string that ends with \n
35 * As a special case to allow proper aligning of multiple chunks
36 * in a patch, a word starting \0 will include 20+ chars with a newline
37 * second from the end.
39 * We make two passes through the stream.
40 * Firstly we count the number of item so an array can be allocated,
41 * then we store start and length of each item in the array
50 #include "ccan/hash/hash.h"
52 static int wiggle_split_internal(char *start, char *end, int type,
62 if ((type & ByWord) && (type & IgnoreBlanks))
64 (*cp == ' ' || *cp == '\t')) {
70 if (*cp == '\0' && cp+19 < end) {
75 switch (type & ByMask) {
77 while (cp < end && *cp != '\n')
83 if (*cp == ' ' || *cp == '\t') {
89 } else if ((type & WholeWord) ||
90 isalnum(*cp) || *cp == '_') {
94 && (((type & WholeWord)
95 && *cp != ' ' && *cp != '\t'
104 if ((type & ByWord) && (type & IgnoreBlanks) &&
105 *start && *start != '\n')
107 (*cp2 == ' ' || *cp2 == '\t' || *cp2 == '\n')) {
114 list->len = cp-start;
115 list->plen = cp2-start;
116 list->prefix = prefix;
118 list->hash = hash(start, list->len, 0);
120 list->hash = atoi(start+1);
129 struct file wiggle_split_stream(struct stream s, int type)
143 cnt = wiggle_split_internal(c, end, type, NULL);
144 f.list = wiggle_xmalloc(cnt*sizeof(struct elmnt));
146 f.elcnt = wiggle_split_internal(c, end, type, f.list);